package com.liuhongwei.test.service.impl;

import java.util.List;
import java.util.ArrayList;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONObject;
import com.liuhongwei.test.entity.vo.JSONRequest;
import com.liuhongwei.test.entity.vo.JSONResponse;
import com.liuhongwei.test.exception.BusinessException;
import com.liuhongwei.test.entity.query.Page;
import com.liuhongwei.test.utils.AppStringUtils;import com.liuhongwei.test.entity.po.SysCountry;
import com.liuhongwei.test.entity.query.SysCountryQuery;
import com.liuhongwei.test.service.SysCountryService;
import com.liuhongwei.test.mapper.SysCountryExtendMapper;

import com.liuhongwei.test.entity.assemble.SysCountryAssemble;
/**
 * 国家表ServiceImpl
 * @auther: 刘洪玮
 * @date: 2025-07-24 11:32
 */
@Service("sysCountryService")
public class SysCountryServiceImpl implements SysCountryService {

	@Resource
	private SysCountryExtendMapper sysCountryExtendMapper;

	/**
	 * 页面分页查询
	 */
	@Override
	public JSONResponse getPage(JSONRequest jsonRequest) throws BusinessException {
		JSONObject jsonObjectData = jsonRequest.getJsonObjectData();
		SysCountryQuery pageQuery = SysCountryAssemble.toPageQuery(jsonObjectData);
		Integer totalSize = sysCountryExtendMapper.selectCount(pageQuery);
		List<JSONObject> pageList = new ArrayList<>();

		if (totalSize > 0) {
			List<SysCountry> pageRecords = sysCountryExtendMapper.selectList(pageQuery);
			for (SysCountry pageRecord : pageRecords) {
				pageList.add(SysCountryAssemble.toPageJSONObject(pageRecord));
			}
		}

		Page<JSONObject> pageResult = new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize(), totalSize, pageList);
		return JSONResponse.success(pageResult);
	}

	/**
	 * 页面新增
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public JSONResponse add(JSONRequest jsonRequest) throws BusinessException {
		JSONObject jsonObjectData = jsonRequest.getJsonObjectData();
		SysCountry sysCountry = SysCountryAssemble.toSysCountryInsert(jsonObjectData);
		sysCountryExtendMapper.insertSelective(sysCountry);
		return JSONResponse.success(new JSONObject());
	}

	/**
	 * 页面更新
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public JSONResponse update(JSONRequest jsonRequest) throws BusinessException {
		JSONObject jsonObjectData = jsonRequest.getJsonObjectData();
		SysCountry sysCountry = SysCountryAssemble.toSysCountryUpdate(jsonObjectData);
		sysCountryExtendMapper.updateByPrimaryKeySelective(sysCountry);
		return JSONResponse.success(new JSONObject());
	}

	/**
	 * 页面详情
	 */
	@Override
	public JSONResponse get(JSONRequest jsonRequest) throws BusinessException {
		JSONObject jsonObjectData = jsonRequest.getJsonObjectData();
		String uuid = AppStringUtils.formatEmptyToNull(jsonObjectData.getString("uuid"));
		SysCountry sysCountry = sysCountryExtendMapper.selectByPrimaryKey(uuid);
		return JSONResponse.success(SysCountryAssemble.toJSONObject(sysCountry));
	}

	/**
	 * 页面删除
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public JSONResponse delete(JSONRequest jsonRequest) throws BusinessException {
		JSONObject jsonObjectData = jsonRequest.getJsonObjectData();
		String uuid = AppStringUtils.formatEmptyToNull(jsonObjectData.getString("uuid"));
		sysCountryExtendMapper.deleteByPrimaryKey(uuid);
		return JSONResponse.success(new JSONObject());
	}

	/**
	 * 新增批量
	 */
	@Override
	public Integer addBatch(List<SysCountry> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.sysCountryExtendMapper.insertBatch(listBean);
	}

	/**
	 * 新增批量或修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<SysCountry> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.sysCountryExtendMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据Uuid查询
	 */
	@Override
	public SysCountry selectByUuid(String uuid) {
		return this.sysCountryExtendMapper.selectByUuid(uuid);
	}

	/**
	 * 根据Uuid更新
	 */
	@Override
	public Integer updateByUuid(SysCountry bean, String uuid) {
		return this.sysCountryExtendMapper.updateByUuid(bean, uuid);
	}

	/**
	 * 根据Uuid删除
	 */
	@Override
	public Integer deleteByUuid(String uuid) {
		return this.sysCountryExtendMapper.deleteByUuid(uuid);
	}

	/**
	 * 根据Code查询
	 */
	@Override
	public SysCountry selectByCode(String code) {
		return this.sysCountryExtendMapper.selectByCode(code);
	}

	/**
	 * 根据Code更新
	 */
	@Override
	public Integer updateByCode(SysCountry bean, String code) {
		return this.sysCountryExtendMapper.updateByCode(bean, code);
	}

	/**
	 * 根据Code删除
	 */
	@Override
	public Integer deleteByCode(String code) {
		return this.sysCountryExtendMapper.deleteByCode(code);
	}

}